#include "knl_threadManager.h"


namespace knl
{
	knl_threadManager::knl_threadManager(void)
	{
		_isDestory = false;
	}


	knl_threadManager::~knl_threadManager(void)
	{
		destory();
	}

	void knl_threadManager::handler( knl_threadRunnablePtr _thread )
	{
		_thread->Job();
	}

	void knl_threadManager::addThreadJob( knl_threadRunnablePtr _runnable,bool _isLoop /*= true */ )
	{
		_runnable->setLoop(_isLoop);
		_group.create_thread(boost::bind(&knl_threadManager::handler,this,_runnable));
		_threadSave.push_back(_runnable);
	}

	void knl_threadManager::join()
	{
		_group.join_all();
	}

	void knl_threadManager::destory()
	{
		if (_isDestory)
			return;

		knl_foreach( knl_threadRunnablePtr vt,_threadSave )
		{
			if (vt)
				vt->setLoop(false);
		}

		_threadSave.clear();
		_isDestory = true;
	}

}